package com.cvic.se.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cvic.se.entity.Institution;
import com.cvic.se.service.InstitutionService;
import com.cvic.se.utils.Result;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * InnoDB free: 4096 kB 前端控制器
 * </p>
 *
 * @author zgx
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/institution")
public class InstitutionController {

	@Autowired
	InstitutionService institutionService;
	
	/**
     * @category 生成机构编号
	 * @return 机构编号 格式 “ZJ00000001”
	 */
	@GetMapping("/generate")
    @ApiOperation(value = "生成机构编号")
	public String getCode() {
		return institutionService.generateCode();
	}
	
	/**
	 * @category 获取全部的机构信息
	 * @return
	 */
	@GetMapping("/getAll")
	@ApiOperation(value = "获取全部部门信息")
	public Result getAllInst() {
		return institutionService.getAllInst();
	}
	
	/**
	 * @category 通过机构编号获取机构信息
	 * @param id 机构编号
	 * @return
	 */
	@GetMapping("/list/{id}")
	@ApiOperation(value = "通过机构编号获取机构信息")
	public Result gatByinstId(@PathVariable("id") String id) {
		return institutionService.getByInstId(id);
	}
	
	/**
	 * @category 添加机构信息
	 * @param inst 机构信息
	 * @return 判断执行添加操作是否成功
	 * */
	@PostMapping("/insertInst")
	@ApiOperation(value = "添加机构信息")
	public Result insertInst(@RequestBody Institution inst) {
		return institutionService.insertInst(inst);
	}
	
	/**
	 * @category 更新机构信息
	 * @param inst 机构信息
	 * @return 判断执行更新操作是否成功
	 * */
	@PostMapping("/updateInst")
	@ApiOperation(value = "更新机构信息")
	public Result updateInst(@RequestBody Institution inst) {
		return institutionService.updateInst(inst);
	}
	
	/**
	 * @category 根据机构的instId值删除指定的机构信息
	 * @param instId 机构的id值
	 * @return 判断执行删除操作是否成功
	 * */
	@DeleteMapping("/deleteInst")
	@ApiOperation(value = "删除机构信息")
	public Result deleteInst(String instId) {
		return institutionService.deleteInst(instId);
	}
	
	/**
	 * @category 分页查询
	 * @param current
	 * @param limit
	 * @return
	 */
	@GetMapping("pageDept/{current}/{limit}")
	@ApiOperation(value = "分页查询")
	public Result pageDepartmentInfo(@PathVariable long current,@PathVariable long limit) {
		Page<Institution> pageInst = new Page<>(current, limit);
		institutionService.page(pageInst, null);
		
		
		long total = pageInst.getTotal();//总记录数
		List<Institution> records = pageInst.getRecords();//数据list集合
		return Result.success().data("total",total).data("rows",records);
	}
}
